#!/bin/bash

[ $# -ge 1 ] || { echo "Usage: $0 /path/to/bundle_dir"; exit 1; }
BUNDLE_DIR=$1
mkdir -p $BUNDLE_DIR/bin
mkdir -p $BUNDLE_DIR/libs
mkdir -p $BUNDLE_DIR/lib
mkdir -p $BUNDLE_DIR/share

FIRE_SCRIPT=${0//bundle/fire}
cp -L $FIRE_SCRIPT $BUNDLE_DIR/fire


GDB_PATH=$(which gdb)
GDB_LIBS=$(ldd $GDB_PATH | grep "=>" | awk '{print $3}' | sort -u)
GDB_LD=$(ldd $GDB_PATH | grep ld-linux | awk '{print $1}')
GDB_SHARE_PATH=$(realpath "$(dirname $GDB_PATH)/../share/gdb")
PYTHON_VERSION=$(ldd $GDB_PATH | grep -oh "python.\.." | head -1)
PYTHON_VERSION_MAJOR=$(ldd $GDB_PATH | grep -oh "python." | head -1)
PYTHON_LIB_PATH=$(realpath "$(dirname $GDB_PATH)/../lib/$PYTHON_VERSION")
PYTHON_SHARE_PATH=$(realpath "$(dirname $GDB_PATH)/../share/$PYTHON_VERSION_MAJOR")
cp -L $GDB_LIBS $BUNDLE_DIR/libs
cp -L $GDB_LD $BUNDLE_DIR/bin
cp -L $GDB_LD $BUNDLE_DIR
cp -L $GDB_PATH $BUNDLE_DIR/bin/
cp -L -r $GDB_SHARE_PATH $BUNDLE_DIR/share
cp -L -r $PYTHON_LIB_PATH $BUNDLE_DIR/lib
cp -L -r $PYTHON_SHARE_PATH $BUNDLE_DIR/share
# This is a dirty hack, but the interpreter must be set to a valid path
patchelf --set-interpreter "$BUNDLE_DIR/bin/$(basename $GDB_LD)" $BUNDLE_DIR/bin/gdb